﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using DSS_XNA.Engine.Error;

namespace DSS_XNA.Engine.World.Worlds.RPGWorld
{
    public class TileSetManager
    {
        #region Properties

        public List<TileSet> TileSets
        {
            get { return _tileSets; }
            set { _tileSets = value; }
        }
        private List<TileSet> _tileSets = new List<TileSet>();

        #endregion

        #region Public Methods

        public TileSetManager()
        {
        }

        public int AddTileSet(string fileName, Game1 engine)
        {
            if (fileName == null)
                throw new DSSException("'fileName' is null.");
            if (fileName.Length == 0)
                throw new DSSException("'fileName' is zero-length.");
            if (engine == null)
                throw new DSSException("'engine' is null.");

            foreach (TileSet ts in this.TileSets)
            {
                if (ts.FileName.ToLower().Trim() == fileName.ToLower().Trim())
                    return this.TileSets.IndexOf(ts);
            }

            fileName = String.Format("worlds\\TileSets\\{0}", fileName);
            fileName = Util.Util.GetFullPath(fileName);
            Texture2D tex = Texture2D.FromFile(engine.GraphicsDevice, fileName);
            if (tex == null)
                throw new DSSException(String.Format("'tex' is null. Failed to load tileset '{0}'",
                    fileName));

            TileSet tileSet = new TileSet();
            tileSet.FileName = fileName;
            tileSet.Texture = tex;
            this.TileSets.Add(tileSet);

            return this.TileSets.Count;
        }

        #endregion
    }
}
